package com.shuqi.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.CheckBookMarkUpdate.CheckBookMarkUpdateLinstener;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.model.bean.CheckBookUpdateInfo;
import com.shuqi.model.bean.CheckPushMessageInfo;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.PushMessageParser;

public class CheckPushMessage {
    private static final String TAG = "CheckPushMessage";
    public static final int TYPE_CHECKRESULT_HASACTION = 5;//推送活动
    
    /** 书签检查更新结果回调接口 */
    public interface CheckPushMessageLinstener {
        /**
         * 书签检查更新结果回调接口
         * 
         * @param resultType 结果返回值
         * @param list 返回成功时有更新的书签
         */
        public void onFinish(int resultType, List<CheckPushMessageInfo> list);
    }
    public static void checkActionAndSingleBook(final Context context,final String userId,
        final CheckPushMessageLinstener linstener) {
    
    HttpRequestListener httpRequestListener = new HttpRequestListener() {
        @Override
        public List<BasicNameValuePair> setParams() {
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
            String timestamp = String.valueOf(System.currentTimeMillis());
            String strTimestamp = timestamp + userId;
            String md5_key = Util.MD5(LoginHelper.Encrypt(strTimestamp));
            params.add(new BasicNameValuePair("PostTime", timestamp));// 时间戳
            params.add(new BasicNameValuePair("PostToken", md5_key));// md5
            return params;
        }

        @Override
        public String getRequestUrl() {
            Log4an.d(TAG, " ===================getRequestUrl()"+Urls.getPushMessageUrl());
            return Urls.getPushMessageUrl();
        }

        @Override
        public void action(int actionCode, Object object) {
            Log4an.d(TAG, " ===================public void action(int actionCode, Object object)");
                List<CheckPushMessageInfo> list = (ArrayList<CheckPushMessageInfo>) object;
                if (list != null && list.size() > 0) {
                    if (linstener != null) {
                        CheckPushMessageInfo info=list.get(0);
                        String pushType=info.getBookId();
                        String title=info.getBookName();
                        String description=info.getUpdateTime();
                        Log4an.d(TAG, "==============pushType=" + pushType + ",title=" + title + ", description=" + description);
                        linstener.onFinish(TYPE_CHECKRESULT_HASACTION, list);
                        
                    }
                } else{
                    Log4an.d(TAG, "=================list为空");
                }
            } 
    };
    HttpRequest request =
            new HttpRequest(context, UrlManager.COLUMN_DOMAIN_APPINFO,
                    httpRequestListener.getRequestUrl(), null,
                    httpRequestListener);
    request.setDataParseAdapter(new PushMessageParser());
    Log4an.d(TAG, " ===============MyTask.runInBackground(request, true)");
    MyTask.runInBackground(request, true);
}}
